﻿using System;
using System.Html.DOM;

namespace RexToy.UI.Controls
{
    public class SingleChoiceButton : ChoiceButton
    {
        private bool __checked;
        public override bool Checked
        {
            get
            {
                return __checked;
            }
            set
            {

                this.__checked = value;
                var dom = this.__domNode.SearchByName("lbl");
                if (value)
                    dom.AppendClass("ui-btn-active");
                else
                    dom.RemoveClass("ui-btn-active");
            }
        }

        public SingleChoiceButton(string text, object value)
            : base(text, value)
        {
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.Div,
                className = "ui-radio",
                items = new DomNodeCreateOptions[]
                {
                    new DomNodeCreateOptions()
                    {
                        tag= Tag.Input,
                        type= InputType.Radio,
                        value= value
                    },
                    new DomNodeCreateOptions()
                    {
                        tag= Tag.Label,
                        hashName= "lbl",
                        className= "ui-btn ui-btn-up-c",
                        items= new DomNodeCreateOptions[]{
                            new DomNodeCreateOptions(){
                                tag= Tag.Span,
                                className= "ui-btn-inner",
                                hashName= "spn",
                                items= new DomNodeCreateOptions[]{
                                    new DomNodeCreateOptions()
                                    {
                                        tag= Tag.Span,
                                        className= "ui-btn-text",
                                        innerHTML= text
                                    }
                                }
                            }
                        }
                    }
                }
            });
            this.__domNode.AttachEvent(DomEvent.OnClick, new Action(this.OnClick).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnClick, new Action(this.OnClick).Bind(this));

            this.__domNode.AttachEvent(DomEvent.OnMouseOver, new Action(this.OnMouseOver).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseOut, new Action(this.OnMouseUp).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseDown, new Action(this.OnMouseDown).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseUp, new Action(this.OnMouseUp).Bind(this));
        }

        private void OnClick()
        {
            if (this.__enable)
            {
                this.Checked = true;
                this.__FireEvent("onchange");
            }
        }
    }
}
